package com.moore.service.impl;

import com.moore.mapper.CommentMapper;
import com.moore.mapper.CommunityMapper;
import com.moore.mapper.LikeMapper;
import com.moore.service.LikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LikeServiceImpl implements LikeService {
    @Autowired
    private LikeMapper likeMapper;
    @Autowired
    private CommunityMapper communityMapper;
    @Autowired
    private CommentMapper commentMapper;

    @Override
    public int addLike(Long communityId, Long candidateId) {
        int result = likeMapper.insertCommunity(communityId, candidateId);
        if (result > 0) {
            communityMapper.addLikeCount(communityId);
            return result;
        } else return 0;
    }

    @Override
    public int addCommentLike(Long communityId, Long commentId, Long candidateId) {
        int result = likeMapper.insertComment(communityId, commentId, candidateId);
        if (result > 0) {
            commentMapper.increaseCommentLikeCount(commentId);
            return result;
        } else return 0;
    }

    @Override
    public int deleteLike(Long communityId, Long candidateId) {
        int result = likeMapper.deleteByCommunityIdAndCandidateId(communityId,candidateId);
        if (result > 0) {
            communityMapper.reduceLikeCount(communityId);
            return result;
        } else return 0;
    }

    @Override
    public int deleteCommentLike(Long communityId, Long commentId, Long candidateId) {
        int result = likeMapper.deleteByCommunityIdAndCommentIdAndCandidateId(communityId,commentId,candidateId);
        if (result > 0) {
            commentMapper.decreaseCommentLikeCount(commentId);
            return result;
        } else return 0;
    }
}
